package nancal.mp


import nbcp.base.extend.*
import nbcp.base.json.*
import nbcp.base.utils.SpringUtil
import nbcp.base.utils.StringUtil
import org.slf4j.LoggerFactory
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.web.context.WebServerApplicationContext
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.context.ApplicationContext
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor
import java.util.*


@SpringBootApplication(
    exclude = arrayOf(
        DataSourceAutoConfiguration::class,
        RabbitAutoConfiguration::class,
        ElasticsearchDataAutoConfiguration::class,
        //RedisAutoConfiguration::class,
        //MongoAutoConfiguration::class
    )
)
// @EnableTokenMvcInterceptor , 如果加上的话，部署就不能用了。
@ConfigurationPropertiesScan
@ServletComponentScan
@EnableDiscoveryClient
//@EnableHystrix
@EnableScheduling
@EnableAsync
@EnableFeignClients(basePackages = arrayOf("nbcp", "nancal"))
//@ConditionalOnProperty("spring.datasource.url")
open class AdminMainApplication {
    companion object {
        val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }
}

fun main(args: Array<String>) {
//    System.setProperty("user.timezone", "Asia/Shanghai");
//    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));

    var context = SpringApplication.run(AdminMainApplication::class.java, *args)

    AdminMainApplication.logger.Important(
        StringUtil.getCenterEachLine(
            """
﹍﹎﹍﹎﹍﹎﹎﹎﹍﹎﹍﹎﹍﹎﹎﹎﹍﹎﹍﹎﹍﹎﹎﹎﹍﹎﹍﹎﹍﹎﹎﹎﹍﹎﹎﹎﹎
${context.debugServerInfo}
﹊﹉﹊﹉﹊﹉﹊﹊﹊﹉﹊﹉﹊﹉﹊﹊﹊﹉﹊﹉﹊﹉﹊﹊﹊﹊﹉﹊﹉﹊﹉﹊﹊﹉﹊﹉﹊
""".split("\n")
        )
            .map { ' '.NewString(6) + it }
            .joinToString("\n")
    )

//    var stream = MainApplication::class.java.classLoader.getResourceAsStream("license.xml")!!
//    try {
//        License().setLicense(stream)
//    } finally {
//        stream.close()
//    }
}

private val ApplicationContext.debugServerInfo: String
    get() {
        var list = mutableListOf<String>()
        if (this is WebServerApplicationContext) {
            var port = this.environment.getProperty("server.port")
            list.add("${this.webServer.javaClass.simpleName}:${port}")
        }
        var applicationName = this.environment.getProperty("spring.application.name")
        var version = this.environment.activeProfiles.joinToString(",")


        list.add("${applicationName}:${version}")

        if (SpringUtil.containsBean<ScheduledAnnotationBeanPostProcessor>()) {
            list.add("@EnableScheduling")
        }


        return list.joinToString(" -- ");
    }

